package com.wlj.rabbit.consume;

import com.rabbitmq.client.Channel;
import com.wlj.rabbit.entity.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @ClassName ConsumerRevice
 * @Description TODO
 * @Author wlj
 * @Date 2018/11/14  14:49
 * @Version 1.0
 */
@Component
public class ConsumerReceive {



    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "message-exchange",durable = "true",type = "topic"),
            value = @Queue(value = "message-queue1",durable = "true"),
            key = "staff.001"
    ))
    @RabbitHandler
    public void receiveQueue1Message(@Payload Message message, @Headers Map<String,Object> headers, Channel channel)throws Exception{
        Long deliveryTag=(Long)headers.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println("接收到队列1: "+message.getId()+"   :"+message.getText());
        channel.basicAck(deliveryTag,false);
    }
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = "message-exchange",durable = "true",type = "topic"),
            value = @Queue(value = "message-queue2",durable = "true"),
            key = "*.一线客服"
    ))
    @RabbitHandler
    public void receiveQueue2Message(@Payload Message message, @Headers Map<String,Object> headers, Channel channel)throws Exception{
        Long deliveryTag=(Long)headers.get(AmqpHeaders.DELIVERY_TAG);
        System.out.println("接收到队列2: "+message.getId()+"   :"+message.getText());
        channel.basicAck(deliveryTag,false);
    }




}
